/**
 * 客户资源录入脚本支持
 */

/** 客户资源KEY * */
var clientKey = "";

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 初始化
	button_bind();

	// 加载最后跟踪状态
	set_last_tracking_state();

	// 按最后跟踪状态进行着色
	set_tracking_state_color();
} );

//--------------------------------------------------------------------------
//
//	consultant_client_first.php
//
//--------------------------------------------------------------------------

function button_bind()
{
	$( ".btnSearchTracking" ).bind( "click", search_tracking );
	remove_tracking();
}

function set_last_tracking_state()
{
	$( "[rel=statelist] a[rel]" ).each( function( id )
	{
		var key = $( this ).attr( "rel" );
		$( this ).click( { key: key }, function( e )
		{
			var stateKey = e.data.key;
			var url_ps = param( location.href );
			var params = [];
			if( !empty( url_ps.key ) )
				params.push( "key=" + url_ps.key );
			if( !empty( stateKey ) )
				params.push( "state=" + stateKey );
			location.href = "?" + params.join( "&" );
		} );
	} );
}

//为表格行添加交替背景色
function setTableRowBackColor( table )
{
	var trs = table.find( "tr:visible[class!=fields][rel!='state_special']" );

	trs.each( function( id )
	{
		$( this ).attr( "class", id % 2 == 0 ? "even_row" : "odd_row" );
	} );
}

function set_tracking_state_color()
{
	// 特殊状态着色
	// 已预约：mldqhlrg1sad
	// 已测评：caoodurhkcl6
	// 已缴费：znouq0wygvbb
	$( "#tbl_tracking tr[rel='data_row']" ).each( function( idx )
	{
		var color = "";
		var keys = [];
		$( this ).find( "[rel=statelist] a[rel]" ).each( function()
		{
			keys.push( $( this ).attr( "rel" ) );
		} );

		if( keys.indexOf( "znouq0wygvbb" ) != -1 )
		{
			// 已缴费
			color = "#B5FCB5";
		}
		else if( keys.indexOf( "caoodurhkcl6" ) != -1 )
		{
			// 已测评
			color = "#D8FFB2";
		}
		else if( keys.indexOf( "mldqhlrg1sad" ) != -1 )
		{
			// 已预约
			color = "#DFDFFF";
		}
		else
		{
			color = "";
		}

		if( color != "" )
		{
			$( this ).attr( "rel", "state_special" );
			$( this ).css( "background-color", color );
			$( this ).find( "td" ).css( "border", "1px solid #f9f9f9" );
		}
	} );

	// 普通隔行着色
	setTableRowBackColor( $( "#tbl_tracking" ) );
}

//--------------------------------------------------------------------------
//
//	数据提交
//
//--------------------------------------------------------------------------

function check_target()
{
	// 检测目标客户
	if( empty( clientKey ) )
	{
		//infopad.err( "无效的目标客户KEY。" );
		return false;
	}

	return true;
}

function search_tracking()
{
	clientKey = $( this ).attr( "rel" );

	if( check_target() == false )
		return;

	submitter.post( "client_tracking_search.php", { key: clientKey }, function( d )
	{
		var v = null;

		if( d.today )
		{
			d[ d.size ] = d.today;
			d.size = d.size + 1;
		}

		if( d.err == 0 && d.size > 0 )
		{
			$( "a[id='btnSearchTracking2']" ).unbind( "click" );

			var h = "<a class='stress unit' onclick='remove_tracking()'> 点击隐藏跟踪记录 </a>";
			h += "<ul>";

			for( var i = 0; i < d.size; i++ )
			{
				v = d[ i ];
				h += "<li>";
				h += "<p class=\"trackingtitle\">第 " + ( i + 1 ) + " 次跟踪记录<em class=\"caption\">（负责人："
						+ v.nickname + "　日期：" + v.mdatens + "）</em><a class='btnHidden'>点击隐藏</a></p>";
				h += "<p class=\"trackingstate\"><b>客户状态</b><span>";

				if( !empty( v.statens ) )
				{
					var ss = v.statens.split( "," );
					for( var j = 0; j < ss.length; j++ )
					{
						h += "<a class=\"stateitem\">" + ss[ j ] + "</a>";
					}

					// 若存在计划跟踪日期，则添加提醒
					if( v.state.indexOf( "138g5wfwm1mx" ) != -1 )
					{
						h += "<a class=\"rect quote\">计划跟踪日期：" + v.remind_date + "</a>";
					}
				}
				else
				{
					h += "<a class=\"stateitem caption\">无变化</a>";
				}

				h += "</span></p>";
				h += "<p><b>备注情况</b></p>";
				h += "<div class=\"content\">" + v.remark + "</div>";
				h += "</li>";
			}

			h += "</ul>";
			add_tracking( clientKey, h );
			$( ".btnHidden" ).click( trackingtitleClickHandler );
			$( ".btnHidden" ).trigger( "click" );
		}
		else
		{
			remove_tracking();
			infopad.warn( "未查询到相关的历史跟踪记录。" );
		}
	}, "json" );
}

function trackingtitleClickHandler( e )
{
	if( $( this ).text() == "点击隐藏" )
	{
		$( this ).text( "点击展开" );
		var $p = $( this ).parent();
		$( this ).parent().parent().children().hide();
		$p.show();
	}
	else
	{
		$( this ).text( "点击隐藏" );
		$( this ).parent().parent().children().show();
	}
}

function add_tracking( key, html )
{
	// 分析取得当前行
	var $btn = $( "a[rel='" + key + "']" ).eq( 0 );
	var $tr = $btn.parent().parent();

	// 添加或插入行到下一行
	$( "#__TRACKING_LIST" ).remove();
	$tr.after( "<tr id='__TRACKING_LIST'><td colspan='10' class='trackinglst'></td></tr>" );
	$( "#__TRACKING_LIST > td" ).html( html );
}

function remove_tracking()
{
	$( "#__TRACKING_LIST" ).remove();
}
